package com.shuorikeji.huohuo.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * 自定义dialog类
 */
public class CustomDialog extends Dialog {
    public CustomDialog(Context context, int theme, int res) {
        super(context, theme);
        setContentView(res);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        // 设置对话框宽度
        Window window = getWindow();
        WindowManager.LayoutParams aWmLp = window.getAttributes();
        aWmLp.width = BusinessUtils.getDeviceWidth(context) * 5 / 6;
        aWmLp.gravity = Gravity.CENTER;
        window.setAttributes(aWmLp);
    }

    public CustomDialog(Context context, int theme, int res, boolean isBottom) {
        super(context, theme);
        setContentView(res);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        // 设置对话框宽度
        Window window = getWindow();
        WindowManager.LayoutParams aWmLp = window.getAttributes();
        aWmLp.width = BusinessUtils.getDeviceWidth(context);
        if (isBottom) {
            aWmLp.gravity = Gravity.BOTTOM;
        } else {
            aWmLp.gravity = Gravity.CENTER;
        }
        window.setAttributes(aWmLp);
    }

    public CustomDialog(Context context, int theme, int res, String flag) {
        super(context, theme);
        setContentView(res);
        // 设置对话框宽度
        Window window = getWindow();
        WindowManager.LayoutParams aWmLp = window.getAttributes();
        if ("share".equals(flag)) {
            aWmLp.gravity = Gravity.BOTTOM;
            aWmLp.width = BusinessUtils.getDeviceWidth(context);
            setCanceledOnTouchOutside(true);
            setCancelable(true);
        } else {
            aWmLp.gravity = Gravity.CENTER;
            aWmLp.width = BusinessUtils.getDeviceWidth(context) * 3 / 4;
            setCanceledOnTouchOutside(false);
            setCancelable(false);
        }
        window.setAttributes(aWmLp);
    }

    /**
     * 弹窗
     *
     * @param context 上下文
     * @param theme   弹窗主题
     * @param res     弹窗布局
     * @param flag    标志位，1：设置高度
     */
    public CustomDialog(Context context, int theme, int res, int flag) {
        super(context, theme);
        setContentView(res);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        // 设置对话框宽度
        Window window = getWindow();
        WindowManager.LayoutParams aWmLp = window.getAttributes();
        aWmLp.width = BusinessUtils.getDeviceWidth(context) * 5 / 6;
        aWmLp.gravity = Gravity.CENTER;
        if (flag == 1) {
            aWmLp.height = BusinessUtils.getDeviceHeight(context) * 7 / 10;
        }
        window.setAttributes(aWmLp);
    }

    /**
     * dialog设置宽高
     *
     * @param context 上下文
     * @param theme   样式
     * @param res     布局
     * @param width   宽比例
     * @param height  高比例
     */
    public CustomDialog(Context context, int theme, int res, double width, double height) {
        super(context, theme);
        setContentView(res);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        // 设置对话框宽度
        Window window = getWindow();
        WindowManager.LayoutParams aWmLp = window.getAttributes();
        aWmLp.width = (int) (BusinessUtils.getDeviceWidth(context) * width);
        if (height == 0.0) {
            aWmLp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        } else {
            aWmLp.height = (int) (BusinessUtils.getDeviceHeight(context) * height);
        }
        aWmLp.gravity = Gravity.CENTER;
        window.setAttributes(aWmLp);
    }

    /**
     * dialog设置宽高
     *
     * @param context  上下文
     * @param theme    样式
     * @param res      布局
     * @param width    宽比例
     * @param height   高比例
     * @param location dialog位置
     */
    public CustomDialog(Context context, int theme, int res, double width, double height, int location) {
        super(context, theme);
        setContentView(res);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        // 设置对话框宽度
        Window window = getWindow();
        WindowManager.LayoutParams aWmLp = window.getAttributes();
        aWmLp.width = (int) (BusinessUtils.getDeviceWidth(context) * width);
        if (height == 0.0) {
            aWmLp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        } else {
            aWmLp.height = (int) (BusinessUtils.getDeviceHeight(context) * height);
        }
        aWmLp.gravity = location;
        window.setAttributes(aWmLp);
    }

    /**
     * dialog设置宽高
     *
     * @param context 上下文
     * @param theme   样式
     * @param res     布局
     * @param height  高比例
     */
    public CustomDialog(Context context, int theme, int res, double height, boolean isCancel) {
        super(context, theme);
        setContentView(res);
        setCanceledOnTouchOutside(isCancel);
        setCancelable(isCancel);
        // 设置对话框宽度
        Window window = getWindow();
        WindowManager.LayoutParams aWmLp = window.getAttributes();
        aWmLp.width = (int) (BusinessUtils.getDeviceWidth(context) * 1);
        if (height == 0.0) {
            aWmLp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        } else {
            aWmLp.height = (int) (BusinessUtils.getDeviceHeight(context) * height);
        }
        aWmLp.gravity = Gravity.BOTTOM;
        window.setAttributes(aWmLp);
    }
}
